Skip to content

MAINT Enable ruff rule E (pycodestyle errors)#1413

Merged
romanlutz merged 4 commits intoAzure:mainfrom
romanlutz:romanlutz/ruff_e
Mar 3, 2026
Merged

MAINT Enable ruff rule E (pycodestyle errors)#1413
romanlutz merged 4 commits intoAzure:mainfrom
romanlutz:romanlutz/ruff_e

Conversation

@romanlutz
Copy link
Contributor

Enable the E rule category covering:

  • E402: module import not at top of file (fixed docstring placement, noqa for intentional)
  • E501: line too long (wrapped long strings/docstrings to 120 chars)
  • E721: type comparison (use 'is' instead of '==')
  • E731: lambda assignment (converted to def)

Copilot AI review requested due to automatic review settings February 27, 2026 17:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This maintenance PR enables the ruff E rule category (pycodestyle errors) in the linting configuration to enforce code style consistency across the PyRIT codebase. The changes address four specific error types: E402 (module imports not at top of file), E501 (lines exceeding 120 characters), E721 (type comparisons using == instead of is), and E731 (lambda assignments instead of def statements).

Changes:

  • Enabled ruff E rule in pyproject.toml to enforce pycodestyle error checks
  • Fixed import placement in 3 files (moved from __future__ import annotations after module docstrings, added noqa comment where intentional)
  • Wrapped long strings and docstrings to comply with 120-character line limit in 11 files
  • Changed type comparison from == to is in test file
  • Converted lambda assignment to proper function definition in discovery module

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pyproject.toml Enabled ruff E rule category in linting configuration
pyrit/scenario/core/scenario_strategy.py Moved from __future__ import annotations after module docstring
pyrit/backend/mappers/attack_mappers.py Moved from __future__ import annotations after module docstring
pyrit/cli/pyrit_backend.py Added noqa:E402 comment for intentional import after sys.stdout.reconfigure
tests/unit/score/test_scorer_prompt_validator.py Shortened docstring to fit 120-character limit
pyrit/score/scorer_evaluation/scorer_metrics_io.py Split long logger.info string across two lines
pyrit/score/audio_transcript_scorer.py Split long logger.info string across two lines
pyrit/scenario/scenarios/airt/jailbreak.py Split long ValueError message across two lines
pyrit/prompt_target/openai/openai_target.py Split long logger.info string across two lines
pyrit/prompt_target/openai/openai_image_target.py Reformatted image_size docstring parameter across multiple lines
pyrit/executor/attack/multi_turn/chunked_request.py Split long request_template docstring across two lines
pyrit/datasets/seed_datasets/remote/vlsu_multimodal_dataset.py Split long User-Agent string across multiple lines using tuple concatenation
pyrit/datasets/jailbreak/text_jailbreak.py Split long ValueError message across two lines
pyrit/cli/pyrit_shell.py Wrapped multiple long strings and command examples with backslash continuations
pyrit/backend/routes/attacks.py Split long Query parameter description across multiple lines
tests/unit/executor/attack/multi_turn/test_red_teaming.py Changed type comparison from == str to is str
pyrit/registry/discovery.py Converted lambda assignment to proper function definition with type hints

Enable the E rule category covering:
- E402: module import not at top of file (fixed docstring placement, noqa for intentional)
- E501: line too long (wrapped long strings/docstrings to 120 chars)
- E721: type comparison (use 'is' instead of '==')
- E731: lambda assignment (converted to def)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jsong468
Copy link
Contributor

jsong468 commented Mar 2, 2026

Enable the E rule category covering:

  • E402: module import not at top of file (fixed docstring placement, noqa for intentional)
  • E501: line too long (wrapped long strings/docstrings to 120 chars)
  • E721: type comparison (use 'is' instead of '==')
  • E731: lambda assignment (converted to def)

noob question: do we already have a pre-commit rule that limits lines to 120 chars?

Copilot AI review requested due to automatic review settings March 2, 2026 23:54
@romanlutz
Copy link
Contributor Author

Enable the E rule category covering:

  • E402: module import not at top of file (fixed docstring placement, noqa for intentional)
  • E501: line too long (wrapped long strings/docstrings to 120 chars)
  • E721: type comparison (use 'is' instead of '==')
  • E731: lambda assignment (converted to def)

noob question: do we already have a pre-commit rule that limits lines to 120 chars?

This is a good question! Copilot:

The E rule group in ruff covers all pycodestyle errors, not just line length. E501 (line too long) is just one of many rules in the E category. The line-length = 120 setting in [tool.ruff] configures ruff's formatter and sets the threshold for E501, but the E rule group also includes things like:

  • E711: comparison to None (use is/is not)
  • E712: comparison to True/False
  • E721: type comparison instead of isinstance()
  • E741: ambiguous variable names
  • E401: multiple imports on one line
  • E711/E712/E721: style issues around comparisons

...and many more. So the "E" rule selection enables enforcement of the full pycodestyle error suite. The existing line-length = 120 config just tells
ruff what the limit is — it doesn't enable the check. Enabling "E" turns on all those checks including E501.

That said, E501 specifically is largely redundant with ruff format (which already wraps lines to 120), but the rest of the E rules catch real code
quality issues that the formatter doesn't address.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

romanlutz and others added 2 commits March 2, 2026 16:08
… main()

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@romanlutz romanlutz merged commit e1353c4 into Azure:main Mar 3, 2026
37 checks passed
@romanlutz romanlutz deleted the romanlutz/ruff_e branch March 3, 2026 00:39
romanlutz added a commit to romanlutz/PyRIT that referenced this pull request Mar 3, 2026
- Add E402, E501 to doc/ per-file-ignores (notebooks inherently have
  mid-file imports and long markdown comment lines)
- Fix E712: replace '== False' with 'not' in doc/ files
- Fix E722: replace bare 'except' with 'except Exception' in doc/ files
- Fix E731: replace lambda with def in test file

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants